package com.ruoyi.hcrf.enums;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import java.util.List;

/**
 * @author wanglf
 */
public class PageFactory {

    public PageFactory() {
    }

    public static <T> Page<T> defaultPage() {
        return new Page<>(1L, 20L);
    }

    public static <T> Page<T> createPage(PageDomain pageCondition) {
        if (ObjectUtil.isNull(pageCondition)) {
            return defaultPage();
        } else {
            int pageSize = 20;
            int pageNo = 1;
            if (ObjectUtil.isNotNull(pageCondition.getPageSize())) {
                pageSize = pageCondition.getPageSize();
            }

            if (ObjectUtil.isNotNull(pageCondition.getPageNum())) {
                pageNo = pageCondition.getPageNum();
            }

            Page<T> page = new Page<>(pageNo, pageSize);
            List<OrderField> orderFields = pageCondition.getOrderByFieldList();

            if (CollectionUtil.isNotEmpty(orderFields)) {
                for (OrderField orderField : orderFields) {
                    if ("asc".equalsIgnoreCase(orderField.getSort())) {
                        page.addOrder(OrderItem.asc(orderField.getFieldName()));
                    } else {
                        page.addOrder(OrderItem.desc(orderField.getFieldName()));
                    }
                }
            }
            return page;
        }
    }
}
